package pers.muci.es.doc;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;

import java.io.IOException;

/**
 * @ClassName DocInsert
 * @Description TODO
 * @Author muci
 * @Date 2021/12/21 21:59
 * @Version 1.0
 **/
public class DocSearchQuery {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("106.14.15.56", 9200, "http"))
        );
        //全量查询
//        SearchRequest request = new SearchRequest();
//        request.indices("user");
//        SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
//        request.source(query);
//        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//        SearchHits hits = response.getHits();
//        System.out.println(hits.getTotalHits());
//        System.out.println(response.getTook());
//        for (SearchHit hit : hits) {
//            System.out.println(hit.getSourceAsString());
//        }

        //条件查询 termQuery age eq 30
//        SearchRequest request = new SearchRequest();
//        request.indices("user");
//        SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.termQuery("age",30));
//        request.source(query);
//        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//        SearchHits hits = response.getHits();
//        System.out.println(hits.getTotalHits());
//        System.out.println(response.getTook());
//        for (SearchHit hit : hits) {
//            System.out.println(hit.getSourceAsString());
//        }

        //分页查询
//        SearchRequest request = new SearchRequest();
//        request.indices("user");
//        SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
//        //(当前页码-1) *每页显示数据条数
//        query.from(0).size(2);
//        request.source(query);
//        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//        SearchHits hits = response.getHits();
//        System.out.println(hits.getTotalHits());
//        System.out.println(response.getTook());
//        for (SearchHit hit : hits) {
//            System.out.println(hit.getSourceAsString());
//        }


        //排序
//        SearchRequest request = new SearchRequest();
//        request.indices("user");
//        SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
//        query.sort("age", SortOrder.DESC);
//        request.source(query);
//        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//        SearchHits hits = response.getHits();
//        System.out.println(hits.getTotalHits());
//        System.out.println(response.getTook());
//        for (SearchHit hit : hits) {
//            System.out.println(hit.getSourceAsString());
//        }

        //过滤字段
//        SearchRequest request = new SearchRequest();
//        request.indices("user");
//        SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
//        String[] excludes = {};
//        String[] includs = {"name"};
//        query.fetchSource(includs, excludes);
//        request.source(query);
//        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//        SearchHits hits = response.getHits();
//        System.out.println(hits.getTotalHits());
//        System.out.println(response.getTook());
//        for (SearchHit hit : hits) {
//            System.out.println(hit.getSourceAsString());
//        }


        //组合查询
//        SearchRequest request = new SearchRequest();
//        request.indices("user");
//        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//
////        boolQueryBuilder.must(QueryBuilders.matchQuery("age",30));
////        boolQueryBuilder.mustNot(QueryBuilders.matchQuery("sex","男"));
////        boolQueryBuilder.must(QueryBuilders.matchQuery("sex","男"));
//
////        boolQueryBuilder.should(QueryBuilders.matchQuery("age", 30));
////        boolQueryBuilder.should(QueryBuilders.matchQuery("age", 40));
//
////        boolQueryBuilder.filter(QueryBuilders.rangeQuery("age").gte(30).lte(40));
//
//        SearchSourceBuilder query = new SearchSourceBuilder().query(boolQueryBuilder);
//        request.source(query);
//        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//        SearchHits hits = response.getHits();
//        System.out.println(hits.getTotalHits());
//        System.out.println(response.getTook());
//        for (SearchHit hit : hits) {
//            System.out.println(hit.getSourceAsString());
//        }

        //模糊查询-高亮查询

//        SearchRequest request = new SearchRequest();
//        request.indices("user");
////        SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.fuzzyQuery("name", "firs").fuzziness(Fuzziness.TWO));
//        SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.termQuery("name", "first")).highlighter(new HighlightBuilder().preTags("<font color='red'>").postTags("</font>").field("name"));
//        request.source(query);
//        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//        SearchHits hits = response.getHits();
//        System.out.println(hits.getTotalHits());
//        System.out.println(response.getTook());
//        for (SearchHit hit : hits) {
//            System.out.println(hit.getSourceAsString());
//        }


        //聚合查询
//        SearchRequest request = new SearchRequest();
//        request.indices("user");
//        SearchSourceBuilder query = new SearchSourceBuilder().aggregation(AggregationBuilders.max("maxAge").field("age"));
//        request.source(query);
//        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//        SearchHits hits = response.getHits();
//        System.out.println(hits.getTotalHits());
//        System.out.println(response.getTook());
//        for (SearchHit hit : hits) {
//            System.out.println(hit.getSourceAsString());
//        }

        //分组查询

        SearchRequest request = new SearchRequest();
        request.indices("user");
        SearchSourceBuilder query = new SearchSourceBuilder().aggregation(AggregationBuilders.terms("ageGroup").field("age"));
        request.source(query);
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        System.out.println(hits.getTotalHits());
        System.out.println(response.getTook());
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }

        client.close();
    }
}
